package com.web.pendingList.controller;

import com.web.common.BaseResult;
import com.web.common.PendingAmend;
import com.web.common.aop.ApproveCheck;
import com.web.common.enums.BaseStatus;
import com.web.common.enums.PendingAction;
import com.web.pendingList.entity.PendingList;
import com.web.pendingList.service.PendingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wenzhuo
 * @create 2024-03-13 2024/3/13
 */
@RestController
@CrossOrigin
@RequestMapping("/pending")
public class PendingListController {

    @Autowired
    private PendingListService pendingListService;

    @RequestMapping(value = "/pendingList")
    public BaseResult<List<PendingList>> getPendingList(@ModelAttribute PendingList pendingList){

        List<PendingList> pendingLists = pendingListService.getPendingList(pendingList.getStatus(), pendingList.getTxType());
        return new BaseResult(BaseStatus.SUCCESS.getCode(),pendingLists);
    }


    @PostMapping(value = "/rejectPending")
    @ApproveCheck
    public BaseResult rejectPendingTxn(@ModelAttribute PendingList pendingList,String userName){
        //根据PendingNumber查询PendingTxn是否存在
        PendingList pendingTxn = pendingListService.getInPending(pendingList.getPendingNum());
        if(pendingTxn == null){
            return new BaseResult<>(BaseStatus.CUSTOMER_NOT_EXIST.getCode(),BaseStatus.CUSTOMER_NOT_EXIST.getDesc());
        }
        //判断申请人和审批人是否一致
        if(userName.equals(pendingTxn.getMaker())){
            return new BaseResult<>(BaseStatus.MAKER_APPROVER_THE_SAME.getCode(),BaseStatus.MAKER_APPROVER_THE_SAME.getDesc());
        }
        //设置审批人
        pendingTxn.setApprover(userName);
        pendingTxn.setRemark(pendingList.getRemark());


        //对新增的审批数据进行Reject
        if(PendingAction.ADD.getAction().equals(pendingTxn.getTxType())){
            //根据Module判断属于什么模块
            PendingAmend pendingAmend = (PendingAmend) pendingListService.checkModule(pendingTxn.getModule());
            if(pendingAmend == null){
                new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
            }
             return  pendingAmend.rejectAddPending(pendingTxn);
        }

        //对刪除的审批数据进行Reject
        if(PendingAction.DEL.getAction().equals(pendingTxn.getTxType())){
            //根据Module判断属于什么模块
            PendingAmend pendingAmend = (PendingAmend) pendingListService.checkModule(pendingTxn.getModule());
            if(pendingAmend == null){
                new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
            }
            return  pendingAmend.rejectDeletePending(pendingTxn);
        }

        //对修改的审批数据进行Reject
        if(PendingAction.MOD.getAction().equals(pendingTxn.getTxType())){
            //根据Module判断属于什么模块
            PendingAmend pendingAmend = (PendingAmend) pendingListService.checkModule(pendingTxn.getModule());
            if(pendingAmend == null){
                new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
            }
            return  pendingAmend.rejectModifyPending(pendingTxn);
        }

        return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
    };


    @PostMapping(value = "/approvePending")
    @ApproveCheck
    public BaseResult approvePendingTxn(@ModelAttribute PendingList pendingList,String userName){

        //根据PendingNumber查询PendingTxn是否存在
        PendingList pendingTxn = pendingListService.getInPending(pendingList.getPendingNum());
        if(pendingTxn == null){
            return new BaseResult<>(BaseStatus.CUSTOMER_NOT_EXIST.getCode(),BaseStatus.CUSTOMER_NOT_EXIST.getDesc());
        }
        //判断申请人和审批人是否一致
        if(userName.equals(pendingTxn.getMaker())){
            return new BaseResult<>(BaseStatus.MAKER_APPROVER_THE_SAME.getCode(),BaseStatus.MAKER_APPROVER_THE_SAME.getDesc());
        }

        //设置审批人
        pendingTxn.setApprover(userName);
        pendingTxn.setRemark(pendingList.getRemark());


        //对新增的审批数据进行Approve
        if(PendingAction.ADD.getAction().equals(pendingTxn.getTxType())){
            //根据Module判断属于什么模块
            PendingAmend pendingAmend =  pendingListService.checkModule(pendingTxn.getModule());
            if(pendingAmend == null){
                new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
            }
            return  pendingAmend.approveAddPending(pendingTxn);
        }

        //对删除的审批数据进行Approve
        if(PendingAction.DEL.getAction().equals(pendingTxn.getTxType())){
            //根据Module判断属于什么模块
            PendingAmend pendingAmend =  pendingListService.checkModule(pendingTxn.getModule());
            if(pendingAmend == null){
                new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
            }
            return  pendingAmend.approveDeletePending(pendingTxn);
        }

        //对修改的审批数据进行Approve
        if(PendingAction.MOD.getAction().equals(pendingTxn.getTxType())){
            //根据Module判断属于什么模块
            PendingAmend pendingAmend = (PendingAmend) pendingListService.checkModule(pendingTxn.getModule());
            if(pendingAmend == null){
                new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
            }

            return  pendingAmend.approveModifyPending(pendingTxn);
        }

        return new BaseResult<>(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
    };
}
